const api = require("../../utils/api")
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'
// pages/comment/comment.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: {},
    columns: ['喜欢', '一般', '不喜欢', '油大', '咸', '淡'],
    showTag: false,
    pickerIndex: ''
  },

  showPicker (e) {
    const index = e.target.dataset.index
    this.setData({
      showTag: true,
      pickerIndex: index
    })
  },
  onConfirmPicker (e) {
    const index = this.data.pickerIndex
    const order = this.data.order
    if (order.orderItemVos[index].comment == null) {
      order.orderItemVos[index].comment =  new Object()
    }
    order.orderItemVos[index].comment.tag = e.detail.value
    this.setData({
      order: order,
      showTag: false
    })
  },
  onCancelPicker () {
    this.setData({
      showTag: false
    })
  },
  changeContent (e) {
    const index = e.target.dataset.index
    const order = this.data.order
    if (order.orderItemVos[index].comment == null) {
      order.orderItemVos[index].comment =  new Object()
    }
    order.orderItemVos[index].comment.content = e.detail
    this.setData({
      order: order
    })
  },

  submitComment () {
    let commentItems = [];
    const orderItemVos = this.data.order.orderItemVos
    for (const orderItemVo of orderItemVos) {
      commentItems.push({
        orderItemId: orderItemVo.id,
        tag: orderItemVo.comment.tag,
        content: orderItemVo.comment.content
      })
    }
    const commentBo = {
      orderId: this.data.order.id,
      commentItems: commentItems
    }
    if (this.data.order.orderStatus === 3) {
      api.put('/order/comment', commentBo).then(res => {
        if (res.code === 200) {
          wx.navigateBack({
            delta: 1,
          })
        }
      })
    } else if (this.data.order.orderStatus === 1) {
      api.post('/order/comment', commentBo).then(res => {
        if (res.code === 200) {
          wx.navigateBack({
            delta: 1,
          })
        }
      })
    }
  },

  deleteComment () {
    Dialog.confirm({
      title: '提示',
      message: '确定删除评价？',
    }).then(() => {
      api.del(`/order/comment/${this.data.order.id}`).then(res => {
        if (res.code === 200) {
          wx.navigateTo({
            url: '/pages/success/success',
          })
        }
      })
    }) .catch(() => {
      // on cancel
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let order = {}
    const eventChannel = this.getOpenerEventChannel()
    // eventChannel.emit('acceptDataFromOpenedPage', {data: order});
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', function(data) {
      order = JSON.parse(JSON.stringify(data.data))
    })
    this.setData({
      order: order
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})